package app.exp;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.rygh.core.model.common.MapList;
import org.rygh.core.util.UIo;
import org.rygh.core.xml.XmlPath;
import org.rygh.core.xml.XmlTree;

import wsi.exp.Exp1SelectedTree;
import wsi.exp.Exp2TreeToEntity;
import wsi.exp.ExpFolderController;
import wsi.exp.ExpXml.Tag;
import wsi.exp.exp3.Exp3PathMatchByPair;
import wsi.exp.exp3.GoldSetMatch;
import wsi.exp.exp3.PathMatch;
import wsi.function.Function;
import wsi.match.MatchProperty;

@SuppressWarnings("deprecation")
public class testExp {

	static void compareExp2() {
		ExpFolderController expf = ExpFolderController.shared();

		Exp2TreeToEntity exp2 = new Exp2TreeToEntity();

		Map<Function, MatchProperty> map1 = new HashMap<>();
		MapList<Function, MatchProperty> map2 = new MapList<>();

		exp2.load2(expf.getGoldFolderForExp2Debug(), map1);
		exp2.loadXml2(expf.getGoldFolderForExp2(), map2);

		assert map1.size() == map2.size();

		for (Function f : map1.keySet()) {
			System.out.println("full name" + f.getFullName());

			assert map2.containsKey(f);

			// assert map1.get(f).equalsTo(map2.get(f));
		}
	}

	static void compareExp3() {
		ExpFolderController expf = ExpFolderController.shared();

		Exp3PathMatchByPair exp3 = new Exp3PathMatchByPair();

		Map<Function, PathMatch> map1 = new HashMap<>();
		Map<Function, PathMatch> map2 = new HashMap<>();

		exp3.load3(expf.getFolderForExp3Debug(), map1);
		exp3.loadXml3(expf.getFolderForExp3(), map2);

		assert map1.size() == map2.size();

		for (Function f : map1.keySet()) {
			System.out.println("full name" + f.getFullName());

			assert map2.containsKey(f);
			assert map1.get(f).equalsTo(map2.get(f));
		}
	}

	static void compareExp3Gold() {
		ExpFolderController expf = ExpFolderController.shared();

		Exp3PathMatchByPair exp3 = new Exp3PathMatchByPair();

		Map<Function, GoldSetMatch> map1 = new HashMap<>();
		Map<Function, GoldSetMatch> map2 = new HashMap<>();

		// exp3.loadGold(expf.getGoldFolderForExp3Debug(), map1);
		// exp3.loadXmlGold(expf.getGoldFolderForExp3(), map2);

		exp3.loadGold(expf.getFolderForExp3DebugCleanDebug(), map1);
		exp3.loadXmlGold(expf.getFolderForExp3DebugClean(), map2);

		assert map1.size() == map2.size();

		for (Function f : map1.keySet()) {
			System.out.println("full name" + f.getFullName());

			assert map2.containsKey(f);
			assert map1.get(f).equalsTo(map2.get(f));
		}
	}

	static void convertExp2() {
		String path = "E:/Downloads/experiments";
		File dir = new File(path);

		Exp1SelectedTree exp1 = new Exp1SelectedTree();
		Map<Function, XmlPath> map1 = new HashMap<>();
		exp1.loadXml1(ExpFolderController.shared().getGoldFolderForExp1(), map1);

		Map<Function, MatchProperty> map2 = new HashMap<>();
		Exp2TreeToEntity exp2 = new Exp2TreeToEntity();
		exp2.load2(dir, map2);

		for (Function function : map2.keySet()) {
			XmlPath rootPath = map1.get(function);
			MatchProperty matchProperty = map2.get(function);

			XmlTree tree = exp2.getXmlTree(rootPath, matchProperty);

			UIo.saveFile(
					new File(dir, String.format("%s_tree_to_entity.xml",
							function.getFullName())), tree.toXmlString());
		}
	}

	static void convertExp3() {
		String path = "E:/Downloads/experiments";
		File dir = new File(path);

		Exp1SelectedTree exp1 = new Exp1SelectedTree();
		Map<Function, XmlPath> map1 = new HashMap<>();
		exp1.loadXml1(ExpFolderController.shared().getFolderForExp1(), map1);

		Map<Function, MatchProperty> map2 = new HashMap<>();
		Exp2TreeToEntity exp2 = new Exp2TreeToEntity();
		exp2.load2(ExpFolderController.shared().getFolderForExp2Debug(), map2);

		Exp3PathMatchByPair exp3 = new Exp3PathMatchByPair();
		// Map<Function, PathMatch> map3 = new HashMap<>();
		// exp3.load3(dir, map3);

		Map<Function, GoldSetMatch> map3 = new HashMap<>();
		exp3.loadGold(dir, map3);

		for (Function function : map3.keySet()) {
			XmlPath rootPath = map1.get(function);
			MatchProperty matchProperty = map2.get(function);
			PathMatch pathMatch = map3.get(function);

			XmlTree tree = exp3.getXmlTree(rootPath, matchProperty, pathMatch,
					false);

			UIo.saveFile(
					new File(dir, String.format("%s_xpath_to_predicate.xml",
							function.getFullName())), tree.toXmlString());
		}
	}

	static void convertExp3B() {
		String path = "E:/Downloads/experiments";
		File dir = new File(path);

		Exp1SelectedTree exp1 = new Exp1SelectedTree();
		Map<Function, XmlPath> map1 = new HashMap<>();
		exp1.loadXml1(ExpFolderController.shared().getFolderForExp1(), map1);

		Map<Function, MatchProperty> map2 = new HashMap<>();
		Exp2TreeToEntity exp2 = new Exp2TreeToEntity();
		exp2.load2(ExpFolderController.shared().getFolderForExp2Debug(), map2);

		File folder = new File(dir, "debug");

		for (File file : folder.listFiles()) {
			String fname = UIo.getFileNameWithoutExtension(file.getName());
			Function function = exp2.parseFileName(fname);

			XmlPath rootPath = map1.get(function);
			// MatchProperty property = map2.get(function);

			String s = UIo.readFile(file);

			s = s.replace(rootPath.stripIndex(), "//");
			// s = "// " + rootPath.stripIndex() + "\r\n" + "// "
			// + property.toString() + "\r\n\r\n" + s;
			s = "// " + rootPath.stripIndex() + "\r\n" + s;

			UIo.saveFile(new File(dir, file.getName()), s);
		}
	}

	static void convertExp4() {
		// Exp4WebCallRatio exp4 = new Exp4WebCallRatio();
		// Map<Function, Exp4WebCallRatio.Tuple> map4 = new HashMap<>();
		// exp4.load4(ExpFolderController.shared().getFolderForExp4(), map4);
		//
		// for (Function function : map4.keySet()) {
		// Tuple tuple = map4.get(function);
		//
		// XmlTree tree = exp4.getXmlTree(tuple.inputSize, tuple.webCall);
		//
		// UIo.saveFile(
		// new File(ExpFolderController.shared().getFolderForExp4(),
		// String.format("%s_call_ratio.xml",
		// function.getFullName())), tree
		// .toXmlString());
		// }
	}

	static void correctPercentage() {
		File dir = ExpFolderController.shared().getFolderForExp3DebugClean();

		for (File f : dir.listFiles()) {
			if (f.isDirectory())
				continue;

			String s = UIo.readFile(f);
			s = s.replaceAll("precentage", Tag.percentage.name());

			UIo.saveFile(f, s);
		}
	}

	public static void main(String[] args) {
	}

}
